/*    file: SdlMouseState.cpp
 *    desc:
 * 
 * created: 2014-01-17 17:06:19
 *  author: zhengchuanjiang
 * company: 
 */

#include "stdafx.h"
#include "SdlMouseState.h"
//#include "libafx.h"
#include <string.h>


namespace sdl
{


MouseState::MouseState():
    m_width(),
    m_height(),
    m_x(),
    m_y()
{

}

MouseState::~MouseState()
{

}

bool MouseState::save(SDL_Window* pWindow)
{
    if (pWindow == NULL)
    {
        pWindow = SDL_GetMouseFocus();
    }

    if (pWindow)
    {
        SDL_GetWindowSize(pWindow, &m_width, &m_height);
    }
    else
    {
        SDL_DisplayMode display;
        memset(&display, 0, sizeof(display));
        SDL_GetDesktopDisplayMode(0, &display);
        m_width = display.w;
        m_height = display.h;
    }

    SDL_GetMouseState(&m_x, &m_y);

    //AppLog::debug("mouse {%d, %d}\n", m_x, m_y);
    return true;
}

bool MouseState::resume(SDL_Window* pWindow)
{
    if (pWindow == NULL)
    {
        pWindow = SDL_GetMouseFocus();
    }

    if (!pWindow)
    {
        return false;
    }


    SDL_Point pt = computeState(pWindow);
    SDL_WarpMouseInWindow(pWindow, pt.x, pt.y);

    //SDL_GetMouseState(&pt.x, &pt.y);
    //AppLog::debug("resume mouse {%d, %d}\n", pt.x, pt.y);

    return (pWindow != NULL);
}

SDL_Point MouseState::computeState(SDL_Window* pWindow)
{
    SDL_Point pt = {m_x, m_y};
    if (pWindow)
    {
        int w = 0;
        int h = 0;
        SDL_GetWindowSize(pWindow, &w, &h);

        if ((w * h > 0) && (m_width * m_height > 0))
        {
            pt.x = w * m_x / m_width;
            pt.y = h * m_y / m_height;
        }
    }
    return pt;
}



}


